// pagesShangping/pages/shoppingDetail/index.js
const app = getApp();
import { getStorage } from "../../../utils/localStorage"
import { shopping_Detail, shoppingCart_Num, shoppingCart_Add, advance_Paymen } from "../../../api/shopping";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //自定义导航栏参数
        navData: {
            title: '商品详情',
            level: 2 //导航栏级别  
        },
        pageTop: app.globalData.navHeight + app.globalData.navTop,
        pageBottom: app.globalData.bottomLift,
        //自定义轮播图组件参数
        swiper: {
            autoplay: true,  //自动切换
            dots: true,  //指示点
            dotsColor: "#ffffff", //指示点颜色
            dotsActiveColor: "#4646A0",//选中指示点颜色
            circular: true, //衔接滑动
            duration: 3000, //滑动动画时长
            boxFlag: false,
        },
        infoData: {
            imageScale: ['https://img2.baidu.com/it/u=3808662287,3833261062&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=667', 'https://img2.baidu.com/it/u=314184602,2246324945&fm=253&fmt=auto&app=120&f=JPEG?w=1000&h=657'],
        },
        //商品id
        id: null,
        spButtonState: false,
        //购物车数量
        spCartNum: 0,
        ljfkButtonState: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            userInfo: getStorage('user')
        });
        if (JSON.stringify(options) != "{}") {
            this.setData({ id: options.id });
            this.getInfo();
        } else {
            wx.showToast({
                icon: 'none',
                title: '获取页面参数失败',
                duration: 2000
            });
        };
        this.getShoppingCartNum();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    //获取页面详细数据
    getInfo() {
        shopping_Detail({ id: this.data.id }).then(res => {
            res.data.data.imageScale = res.data.data.imageScale ? res.data.data.imageScale.split(",") : [];
            this.setData({ infoData: res.data.data });
        });
    },
    //获取购物车数量
    getShoppingCartNum() {
        shoppingCart_Num({ userId: this.data.userInfo.id }).then(res => {
            this.setData({ spCartNum: res.data.data });
        });
    },
    //购物按钮点击事件
    gwcTap() {
       wx.redirectTo({
        url: '/pagesShangping/pages/index/index?pageIndex=' + 2,
       });
    },
    //加入购物车按钮点击事件
    addShoppingTap() {
        this.setData({ spButtonState: true });
        shoppingCart_Add({ goodsId: this.data.id, userId: this.data.userInfo.id, goodsCount: 1 }).then(res => {
            if(res.data.code === 200){
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 2000
                });
                setTimeout(() => {
                    this.setData({ spButtonState: false });
                    this.getShoppingCartNum();
                }, 2000);
            }else{
                this.setData({ spButtonState: false });
            };
        });

    },
    //立即购物按钮点击事件
    showNewTap() {
        this.setData({ ljfkButtonState: true });
        this.data.infoData.imageScale = this.data.infoData.imageScale ? this.data.infoData.imageScale.join(",") : "";
        this.data.infoData.userId = this.data.userInfo.id;
        this.data.infoData.goodsId = this.data.infoData.id;
        advance_Paymen([this.data.infoData]).then(res => {
            if (res.data.code === 200) {
                setTimeout(() => {
                    wx.navigateTo({
                        url: '/pagesShangping/pages/submitOrder/index?data=' + encodeURIComponent(JSON.stringify(res.data.data)),
                    });
                }, 1000);
            } else {
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                    duration: 2000
                });
            };
            setTimeout(() => { this.setData({ ljfkButtonState: false }); }, 2000);
        });
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})